package com.szrt.ch04.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.szrt.ch04.dto.PageDTO;
import com.szrt.ch04.entity.UserEntity;
import com.szrt.ch04.service.UserService;
import com.szrt.ch04.service.impl.UserServiceImpl;


//servlet 2.5
public class UserController extends HttpServlet {
	
	
	
	private UserService service = new UserServiceImpl();
	
	public UserController(){
		
		System.out.println("调用构造方法...");
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		
		
		String money =config.getInitParameter("money");
		System.out.println(money);
		
		String age =config.getInitParameter("age");
		System.out.println(age);
		
		
		Enumeration<String> es = config.getInitParameterNames();
		
		while(es.hasMoreElements()){
			
			String key = es.nextElement();
			
			String value = config.getInitParameter(key);
			
			System.out.println(key + ": " + value);
			
		}
		
		System.out.println("进入init方法....");
	}
	
	
	@Override
	public void destroy() {
		System.out.println("进入destroy方法....");
	}

	private static final long serialVersionUID = -6955896479800559179L;
	
	
	private void findWithPage(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
		String currentPage =request.getParameter("currentPage");
		String pageSize =request.getParameter("pageSize");
		
		PageDTO page = new PageDTO(Integer.parseInt(currentPage),Integer.parseInt(pageSize));
		
		
		// controller  1：接受参数   2 把service的处理结果丢到前台去 以为这controller层不会做逻辑处理
		
		// service  所有的逻辑处理由service来完成   业务逻辑层   
		
		
		// 新手容易犯的错误   把所有的业务处理代码 写 在  controller 错误 ！！！
		Map<String, Object> jieguo = service.findWithPage(page);
		
		
		
		Gson json = new Gson();
		String jsonString = json.toJson(jieguo);
		
		Writer out = response.getWriter();
		out.write(jsonString);
		
	}
	
	private void getPlayCoin(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
		//军规：凡是http协议传送的数据只有1中类型  字符串
		String age =request.getParameter("age");
		String playCoin;
		if(Integer.parseInt(age)>=18){
			playCoin ="500";
		}else{
			playCoin ="100";
		}
		System.out.println(playCoin);
		
		
		UserEntity  user= new UserEntity();
		user.setId(1);
		user.setPhone("13926583118");
		user.setPassword("e10adc3949ba59abbe56e057f20f883e");
		
		
		/*UserEntity  user2= new UserEntity();
		user2.setId(2);
		user2.setPhone("13926583119");
		user2.setPassword("e10adc3949ba59abbe56e057f20f8834");
		*/
		
		
		
		
		
		
		
		//写到 response
		Map<String, Object> jieguo  = new HashMap<String, Object>();
		jieguo.put("message", "请求成功...");
		jieguo.put("playCoin", playCoin);
		jieguo.put("user", user);
		
		
		Gson json = new Gson();
		String jsonString = json.toJson(jieguo);
		
		Writer out = response.getWriter();
		out.write(jsonString);
		
	}
	
	
	/**
	 * 
	 * 1  post +  req.setCharacterEncoding("utf-8");
	 * 
	 * 2  get  + 编码转化 String ok = new String(luanma.getBytes("iso-8859-1"),"utf-8");
	 * 
	 * 3  过滤器 
	 * 
	 * 4 框架
	 */
	@SuppressWarnings("unused")
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse response) 
			throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		//如果没写会有乱码问题
		response.setContentType("text/html;charset=utf-8");
		
		
		
		
		
		//1 根据请求的不同调用不同的方法
		
		String method = req.getParameter("method");
		
		System.out.println(method);
		if("getPlayCoin".equals(method)){
			
			getPlayCoin(req,response);
			
			return;
		}else if("findWithPage".equals(method)){
			findWithPage(req,response);
			return ;
		}
		
		
		//2如何把一个值传送到前台
		
		//取值开始
		
		
		
		// 用户表插入1条  
		
		// 爱好表  插入 多条    USER_ID  ?  动态生成id  多张表如何控制事物?
		
		// $.post $.ajax
		
		// Service  Dao   实现类
		
		
		// 列表  4     分页
		
		System.out.println("==============================");
		
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String sex = req.getParameter("sex");
		String hobbies = req.getParameter("hobbies");
		String city = req.getParameter("city");
		String money = req.getParameter("money");
		String detail = req.getParameter("detail");
		
		
		
		//取值结束
		
		
		// int counter = service.add(UserEntity entity);
		//如何返回1个结果给到前台  告诉前台 插入成功
		
		//固定的套路
		
		
		//插入到数据中
		
		
		
		//字符流
		Writer out = response.getWriter();
		
		
		
		// 影响的记录行数  1/0  前后台用json交换数据
		// 插入成功
		
		Map<String, String> jieguo  = new HashMap<String, String>();
		jieguo.put("counter", "1");
		jieguo.put("message", "插入成功...");
		jieguo.put("age", "18");
		Gson json = new Gson();
		String jsonString = json.toJson(jieguo);
		
		
		out.write(jsonString);
		
		
	}

	

}
